package sivan.process;

import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValue;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;

import java.util.Iterator;

/**
 * Created by sivan on 2019/2/25.
 * refresh()方法的invokeBeanFactoryPostProcessors调用
 * 顺序是先调用BeanDefinitionRegistryPostProcessor的后置处理器
 * 在调用BeanFactoryPostProcessor的后置处理器
 */
public class CustomBeanFactoryPostProcessor implements BeanFactoryPostProcessor{

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        System.out.println("调用了自定义的BeanFactoryPostProcessor " + beanFactory);
        Iterator it = beanFactory.getBeanNamesIterator();

        String[] names = beanFactory.getBeanDefinitionNames();
        // 获取了所有的bean名称列表
        for(int i=0; i<names.length; i++){
            String name = names[i];

            BeanDefinition bd = beanFactory.getBeanDefinition(name);
            System.out.println(name + " bean properties: " + bd.getPropertyValues().toString());
            // 本内容只是个demo，打印持有的bean的属性情况
        }
        BeanDefinition beanDefinition = beanFactory.getBeanDefinition("person");
        PropertyValue propertyValue=new PropertyValue("userName", "掘墓人");
        beanDefinition.getPropertyValues().addPropertyValue(propertyValue);
    }
}
